const { add, find, update, del,addAll } = require("../../server")
const systemconfiguration = require('../../model/systemconfiguration')
const systemconfiguration1 = require('../../model/systemconfiguration1')


//查询全部数据 
const getCtr = async (req, res, next) => {
  try {
    let data = await find(systemconfiguration)
    res.send({
      code: 200,
      data,
    })
  } catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: '数据查询失败',
    })
  }
}

//模糊查询
const getCtr1 = async (req, res, next) => {
  let tiao = req.query
  try {
    let data = await find(systemconfiguration, {
      nickname: tiao.nickname
    })
      res.send({
        code: 200,
        data: data
      })
  
  }
  catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: '数据查询失败',
    })
  }
}

//新增 
const postCtr = async (req, res, next) => {
  let data = req.body
  try {
    await add(systemconfiguration, data)
    res.send({
      code: 200,
      message: '数据添加成功'
    })
  } catch (error) {
    res.send({
      code: 400,
      message: '数据添加失败'
    })
  }
}

// 文件导入
const postCtr1 = async (req, res, next) => {
  let data = req.body;
  try {
    await addAll(systemconfiguration, data);
    res.send({
      code: 200,
      message: "数据插入成功",
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: "数据插入失败",
    });
  }
};

// 信息修改
const putCtr = async (req, res, next) => {
  let data = req.body
  try {
    let bob = await update(systemconfiguration, {
      description:data.description,
      keyValue: data.keyValue,
      createdTime: data.createdTime,
      nickname: data.nickname,
    }, {
      id: data.id
    })
    if (bob == 0) {
      res.send({
        code: 400,
        message: '未进行修改操作'
      })
      return;
    }
    res.send({
      code: 200,
      message: '修改成功!'
    })
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: '抱歉,修改失败!'
    })
  }

}

// 删除数据
const delCtr = async (req, res, next) => {
  try {
    let bob = await del(systemconfiguration, {
      id: req.body.id
    })
    if (bob == 0) {
      res.send({
        code: 400,
        message: '删除失败，请联系程序员'
      })
      return;
    }
    res.send({
      code: 200,
      message: '删除成功'
    })
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: '删除失败，请联系程序员'
    })
  }
}

//========================回收站=============================
//查询全部数据 
const getRecycle = async (req, res, next) => {
  try {
    let data = await find(systemconfiguration1)
    res.send({
      code: 200,
      data,
    })
  } catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: '数据查询失败',
    })
  }
}

//新增 
const postRecycle = async (req, res, next) => {
  let data = req.body
  try {
    await add(systemconfiguration1, data)
    res.send({
      code: 200,
      message: '数据添加成功'
    })
  } catch (error) {
    res.send({
      code: 400,
      message: '数据添加失败'
    })
  }
}

//模糊查询
const getRecycles = async (req, res, next) => {
  let tiao = req.query
  try {
    let data = await find(systemconfiguration1, {
      nickname: tiao.nickname
    })
      res.send({
        code: 200,
        data: data
      })
  
  }
  catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: '数据查询失败',
    })
  }
}

// 删除数据
const delRecycle = async (req, res, next) => {
  try {
    let bob = await del(systemconfiguration1, {
      id: req.body.id
    })
    if (bob == 0) {
      res.send({
        code: 400,
        message: '删除失败，请联系程序员'
      })
      return;
    }
    res.send({
      code: 200,
      message: '删除成功'
    })
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: '删除失败，请联系程序员'
    })
  }
}

// 恢复数据
const delRecycles = async (req, res, next) => {
  try {
    let bob = await del(systemconfiguration1, {
      id: req.body.id
    })
    if (bob == 0) {
      res.send({
        code: 400,
        message: '恢复失败，请联系程序员'
      })
      return;
    }
    res.send({
      code: 200,
      message: '恢复成功'
    })
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: '恢复失败，请联系程序员'
    })
  }
}
module.exports = {
  getCtr, getCtr1,postCtr,putCtr,delCtr,postCtr1,getRecycle,postRecycle,getRecycles,delRecycle,delRecycles
}